package com.zhuo.schoolproject.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.zhuo.schoolproject.comment.Valida;
import com.zhuo.schoolproject.entitys.LoginUser;
import com.zhuo.schoolproject.entitys.User;
import com.zhuo.schoolproject.mapper.UserMapper;
import com.zhuo.schoolproject.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.xml.transform.sax.SAXResult;

import java.util.Map;

import static net.sf.jsqlparser.util.validation.metadata.NamedObject.user;

@RestController
@RequestMapping("/user/")
@RequiredArgsConstructor
public class UserController {
    private UserService userService;

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("login")
    public SaResult login(@RequestBody LoginUser user){
        return userService.login(user);
    }

    /**
     *
     */
    @GetMapping("logput")
    public SaResult logput(){
        StpUtil.logout();
        return SaResult.ok("退出成功");
    }

    @GetMapping("id")
    public SaResult selectUser(@RequestParam Long id){
        return userService.selectUser(id);
    }

    @PostMapping("update")
    public SaResult updateUser(@Validated(Valida.Update.class) @RequestBody User user){
        return userService.updateUser(user);
    }

    @PostMapping("saveUser")
    public SaResult saveUser(@Validated(Valida.Create.class)@RequestBody User user){
        return userService.saveUser(user);
    }
    @GetMapping("delelte")
    public SaResult deleteUser(@RequestParam Long id){
        return userService.deleteUser(id);
    }

    @GetMapping("page")
    public SaResult selectPage(@RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size, @RequestParam Map<String,Object> search){
        return userService.selectPage(current,size,search);
    }

}
